<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <!--            <a-col :md="8" :sm="24">-->
            <!--              <a-form-item label="活动分类" prop="activityType">-->
            <!--                <a-select :key="queryParam.activityType" placeholder="请选择活动分类" v-model="queryParam.activityType" style="width: 100%" allow-clear>-->
            <!--                  <a-select-option>请选择字典生成</a-select-option>-->
            <!--                </a-select>-->
            <!--              </a-form-item>-->
            <!--            </a-col>-->
            <a-col :md="8" :sm="24">
              <a-form-item label="活动状态" prop="activityStatus">
                <a-select placeholder="请选择活动状态" v-model="queryParam.activityStatus" style="width: 100%" allow-clear>
                  <!--                  <a-select-option>请选择字典生成</a-select-option>-->
                  <a-select-option v-for="(d, index) in activityStatusOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="活动名称" prop="activityName">
                <a-input v-model="queryParam.activityName" placeholder="请输入活动名称" allow-clear/>
              </a-form-item>
            </a-col>

            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="联系方式" prop="activityPhone">
                  <a-input v-model="queryParam.activityPhone" placeholder="请输入联系方式" allow-clear/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="活动时间" prop="activityCreateTime">
                  <!--                  <a-date-picker style="width: 100%" v-model="queryParam.activityCreateTime" format="YYYY-MM-DD" allow-clear/>-->
                  <a-range-picker :show-time="timeOptions" format="YYYY-MM-DD HH:mm" @change="queryTime" />
                </a-form-item>
              </a-col>
              <!--              <a-col :md="8" :sm="24">-->
              <!--                <a-form-item label="活动更新时间" prop="activityUpdateTime">-->
              <!--                  <a-date-picker style="width: 100%" v-model="queryParam.activityUpdateTime" format="YYYY-MM-DD" allow-clear/>-->
              <!--                </a-form-item>-->
              <!--              </a-col>-->
              <!--              <a-col :md="8" :sm="24">-->
              <!--                <a-form-item label="活动人数" prop="activityNumber">-->
              <!--                  <a-input v-model="queryParam.activityNumber" placeholder="请输入活动人数" allow-clear/>-->
              <!--                </a-form-item>-->
              <!--              </a-col>-->
              <!--              <a-col :md="8" :sm="24">-->
              <!--                <a-form-item label="活动已报名人数" prop="activityEnterNumber">-->
              <!--                  <a-input v-model="queryParam.activityEnterNumber" placeholder="请输入活动已报名人数" allow-clear/>-->
              <!--                </a-form-item>-->
              <!--              </a-col>-->

            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作 -->
      <div class="table-operations">
        <a-button type="primary" @click="$refs.createForm.handleAdd()" v-hasPermi="['angel:activity:add']">
          <a-icon type="plus" />新增
        </a-button>
        <a-button type="primary" :disabled="single" @click="$refs.createForm.handleUpdate(undefined, ids)" v-hasPermi="['angel:activity:edit']">
          <a-icon type="edit" />修改
        </a-button>
        <a-button type="danger" :disabled="multiple" @click="handleDelete" v-hasPermi="['angel:activity:remove']">
          <a-icon type="delete" />删除
        </a-button>
        <a-button type="primary" @click="handleExport" v-hasPermi="['angel:activity:export']">
          <a-icon type="download" />导出
        </a-button>
        <a-button
          type="dashed"
          shape="circle"
          :loading="loading"
          :style="{float: 'right'}"
          icon="reload"
          @click="getList" />
      </div>
      <!-- 增加修改 -->
      <create-form
        ref="createForm"
        @ok="getList"
      />
      <!-- 数据展示 -->
      <a-table
        :loading="loading"
        :size="tableSize"
        rowKey="activityId"
        :columns="columns"
        :data-source="list"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :pagination="false">
        <span slot="activityCreateTime" slot-scope="text, record">
          {{ parseTime(record.activityCreateTime) }}
        </span>
        <span slot="activityUpdateTime" slot-scope="text, record">
          {{ parseTime(record.activityUpdateTime) }}
        </span>
        <span slot="activityStatus" slot-scope="text, record">
          <a-tag color="blue" v-if="text === 0">
            {{ activityStatusFormat(record) }}
          </a-tag>
          <a-tag color="orange" v-if="text === 1">
            {{ activityStatusFormat(record) }}
          </a-tag>
          <a-tag color="green" v-if="text === 2">
            {{ activityStatusFormat(record) }}
          </a-tag>
        </span>
        <span slot="operation" slot-scope="text, record">
          <a-divider type="vertical" v-hasPermi="['angel:activity:edit']" />
          <a @click="showDrawer(record)" v-hasPermi="['angel:activity:info']">
            <a-icon type="unordered-list" />详情
          </a>
          <a-divider type="vertical" v-hasPermi="['angel:activity:edit']" />
          <a @click="$refs.createForm.handleUpdate(record, undefined)" v-hasPermi="['angel:activity:edit']">
            <a-icon type="edit" />修改
          </a>
          <a-divider type="vertical" v-hasPermi="['angel:activity:remove']" />
          <a @click="handleDelete(record)" v-hasPermi="['angel:activity:remove']">
            <a-icon type="delete" />删除
          </a>
        </span>
      </a-table>
      <!-- 分页 -->
      <a-pagination
        class="ant-table-pagination"
        show-size-changer
        show-quick-jumper
        :current="queryParam.pageNum"
        :total="total"
        :page-size="queryParam.pageSize"
        :showTotal="total => `共 ${total} 条`"
        @showSizeChange="onShowSizeChange"
        @change="changeSize"
      />
    </a-card>
    <a-drawer
      width="50%"
      title="活动详情"
      placement="right"
      :closable="false"
      :visible="visible"
      @close="onClose"
    >
      <a-descriptions :title="this.selectNode.activityName">
        <a-descriptions-item v-if="selectNode.activityStatus === 0" label="活动状态">
          <a-tag color="blue">未开始</a-tag>
        </a-descriptions-item>
        <a-descriptions-item v-if="selectNode.activityStatus === 1" label="活动状态">
          <a-tag color="orange">进行中</a-tag>
        </a-descriptions-item>
        <a-descriptions-item v-if="selectNode.activityStatus === 2" label="活动状态">
          <a-tag color="green">已结束</a-tag>
        </a-descriptions-item>
        <a-descriptions-item label="活动人数">
          {{ selectNode.activityNumber }} 人
        </a-descriptions-item>
        <a-descriptions-item label="已报名人数">
          {{ selectNode.activityEnterNumber }} 人
        </a-descriptions-item>
        <a-descriptions-item label="活动开始时间">
          {{ parseTime(selectNode.activityCreateTime) }}
        </a-descriptions-item>
        <a-descriptions-item label="活动结束时间">
          {{ parseTime(selectNode.activityUpdateTime) }}
        </a-descriptions-item>
        <a-descriptions-item>
        </a-descriptions-item>
        <a-descriptions-item label="活动内容描述">
          {{ selectNode.activityContent }}
        </a-descriptions-item>
      </a-descriptions>

      <a-divider>报名成员详情</a-divider>

      <a-list :loading="drawerLoading" item-layout="horizontal" :data-source="memberList">
        <a-list-item slot="renderItem" slot-scope="item">
          <a-list-item-meta>
            <span slot="title">
              {{ item.memberName }}
              <a-tag v-if="item.memberType === 0" color="purple">
                普通会员
              </a-tag>
              <a-tag v-if="item.memberType === 1" color="orange">
                志愿者
              </a-tag>
              <a-tag v-if="item.memberType === 1" color="purple">
                <a-icon type="clock-circle" />
              </a-tag>
              <a-tag v-if="item.memberType === 2" color="orange">
                志愿者
              </a-tag>
              <a-tag v-if="item.memberType === 2" color="green">
                <a-icon type="check-circle" />
              </a-tag>
              <a-tag v-if="item.memberType === 3" color="orange">
                志愿者
              </a-tag>
              <a-tag v-if="item.memberType === 3" color="red">
                <a-icon type="close-circle" />
              </a-tag>
              <a-tag v-if="item.memberType === 4" color="blue">
                管理员
              </a-tag>
            </span>
            <span slot="description">
              学号: {{ item.numberNum }} <a-divider type="vertical" />
              专业: {{ item.subjectName }} <a-divider type="vertical" />
              班级: {{ item.memberClass }} <a-divider type="vertical" />
              时长: {{ item.memberDuration }} 小时
            </span>
            <a-avatar slot="avatar" :src="item.memberAvatarUrl"/>
          </a-list-item-meta>
          <a v-if="showRemoveA" @click="removeMemberAtActivity(item)" v-hasPermi="['angel:activity:remove']">
            <a-icon type="delete" />移出活动
          </a>
        </a-list-item>
      </a-list>
    </a-drawer>
  </page-header-wrapper>
</template>

<script>
import { listActivity, delActivity, exportActivity, getActivityMember, removeMemberAtActivity } from '@/api/angel/activity'
import CreateForm from './modules/CreateForm'

export default {
  // TODO     活动更改到今天时间后要执行一次扫描,或者直接添加到扫描表中
  // TODO    修改时间后同时修改扫描表
  name: 'Activity',
  components: {
    CreateForm
  },
  data () {
    return {
      visible: false,
      drawerLoading: false,
      timeOptions: {
        format: 'HH:mm',
        'minute-step': 15
      },
      list: [],
      memberList: [],
      showRemoveA: false,
      selectNode: {
        activityId: null,
        activityType: null,
        activityPhone: null,
        activityCreateTime: null,
        activityStatus: 0,
        activityContent: null,
        activityNumber: null,
        activityEnterNumber: null,
        activityUpdateTime: null,
        activityName: null
      },
      value: '',
      selectedRowKeys: [],
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      ids: [],
      activityStatusOptions: [],
      loading: false,
      total: 0,
      // 查询参数
      queryParam: {
        activityType: null,
        activityPhone: null,
        activityCreateTime: null,
        activityStatus: null,
        activityContent: null,
        activityNumber: null,
        activityEnterNumber: null,
        activityUpdateTime: null,
        activityName: null,
        pageNum: 1,
        pageSize: 10
      },
      columns: [
        // {
        //   title: '活动主键',
        //   dataIndex: 'activityId',
        //   ellipsis: true,
        //   align: 'center'
        // },
        // {
        //   title: '活动分类',
        //   dataIndex: 'activityType',
        //   ellipsis: true,
        //   align: 'center'
        // },
        {
          title: '活动名称',
          dataIndex: 'activityName',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '联系方式',
          dataIndex: 'activityPhone',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '活动状态',
          dataIndex: 'activityStatus',
          scopedSlots: { customRender: 'activityStatus' },
          ellipsis: true,
          align: 'center'
        },
        {
          title: '活动人数',
          dataIndex: 'activityNumber',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '活动已报名人数',
          dataIndex: 'activityEnterNumber',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '活动开始时间',
          dataIndex: 'activityCreateTime',
          scopedSlots: { customRender: 'activityCreateTime' },
          ellipsis: true,
          align: 'center'
        },
        {
          title: '活动结束时间',
          dataIndex: 'activityUpdateTime',
          scopedSlots: { customRender: 'activityUpdateTime' },
          ellipsis: true,
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: '18%',
          scopedSlots: { customRender: 'operation' },
          align: 'center'
        }
      ]
    }
  },
  filters: {
  },
  created () {
    this.getList()
    this.getDicts('activity_status').then(response => {
      this.activityStatusOptions = response.data
    })
  },
  computed: {
  },
  watch: {
  },
  methods: {
    queryTime (date, dateString) {
      const dateArr = dateString
      this.queryParam.activityCreateTime = dateArr[0]
      this.queryParam.activityUpdateTime = dateArr[1]
    },
    removeMemberAtActivity (item) {
      const that = this
      const activityId = this.selectNode.activityId
      const memberId = item.memberId
      this.$confirm({
        title: '确认删除所选中数据?',
        content: '当前选中姓名为' + item.memberName + '的数据',
        onOk () {
          return removeMemberAtActivity(memberId, activityId)
            .then(() => {
              that.getMemberList(activityId)
              that.$message.success(
                '删除成功',
                3
              )
            }).catch((err) => {
              that.$message.error(
                err + ',活动开始后不可以移除成员',
                3
              )
            })
        },
        onCancel () {}
      })
    },
    /** 查询活动列表 */
    getList () {
      this.loading = true
      listActivity(this.queryParam).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    activityStatusFormat (row, column) {
      return this.selectDictLabel(this.activityStatusOptions, row.activityStatus)
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        activityType: undefined,
        activityPhone: undefined,
        activityCreateTime: undefined,
        activityStatus: undefined,
        activityContent: undefined,
        activityNumber: undefined,
        activityEnterNumber: undefined,
        activityUpdateTime: undefined,
        activityName: undefined,
        pageNum: 1,
        pageSize: 10
      }
      this.handleQuery()
    },
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.activityId)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      var that = this
      const activityIds = row.activityId || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        content: '当前选中编号为' + activityIds + '的数据',
        onOk () {
          return delActivity(activityIds)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    async showDrawer (record) {
      this.selectNode = { ...record }
      await this.getMemberList(record.activityId)
      const activityStartTime = this.parseTime(record.activityCreateTime)
      const currentTime = this.parseTime(new Date())
      this.showRemoveA = currentTime < activityStartTime
      this.visible = true
    },
    getMemberList (activityId) {
      this.drawerLoading = true
      getActivityMember(activityId).then(response => {
        this.memberList = response.data
        this.drawerLoading = false
      })
    },
    onClose () {
      this.visible = false
      this.reset()
    },
    reset () {
      this.selectNode = {
        activityId: null,
        activityType: null,
        activityPhone: null,
        activityCreateTime: null,
        activityStatus: 0,
        activityContent: null,
        activityNumber: null,
        activityEnterNumber: null,
        activityUpdateTime: null,
        activityName: null
      }
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          return exportActivity(that.queryParam)
            .then(response => {
              that.download(response.msg)
              that.$message.success(
                '导出成功',
                3
              )
          })
        },
        onCancel () {}
      })
    }
  }
}
</script>
